﻿using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client
{
    public partial class ChooseForm_SpecialityKinds : BaseForm
    {
        public string strSelectType = "";
        BLL.ServiceComm service = new BLL.ServiceComm();
        public ChooseForm_SpecialityKinds()
        {
            InitializeComponent();
            BindExamPlace();
        }
        public ChooseForm_SpecialityKinds(string strType)
        {
            InitializeComponent();
            strSelectType = strType;
            BindExamPlace();
        }

        private void BindExamSpecNo()
        {
            if (strSelectType == "")
            {
                if (listExamPlace.SelectedIndex < 0)
                {
                    return;
                }
                string ExamPlaceID = string.Empty;
                string ExamPlaceName = string.Empty;
                if (listExamPlace.SelectedItem != null)
                {
                    DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                    ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                    ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
                }
                using (DataTable dt = service.GetSpecialityKindsTableByExamPlaceID_CS(ExamPlaceID))
                {
                    if (dt != null)
                    {
                        DataTable dtNew = new DataTable();
                        dtNew.Columns.Add("SpecialityKindsNO", typeof(string));
                        dtNew.Columns.Add("SpecialityKindsName", typeof(string));
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dtNew.Select("SpecialityKindsNO='" + dr["SpecialityKindsID"].ToSafeString() + "'").Length == 0)
                            {
                                DataRow drNew = dtNew.NewRow();
                                drNew["SpecialityKindsNO"] = dr["SpecialityKindsID"];
                                drNew["SpecialityKindsName"] = dr["SpecialityKindsNameAS"];
                                dtNew.Rows.Add(drNew);
                            }
                        }
                        listSpeciality.DisplayMember = "SpecialityKindsName";
                        listSpeciality.ValueMember = "SpecialityKindsNO";
                        listSpeciality.DataSource = dtNew;
                    }
                    else
                    {
                        listSpeciality.DisplayMember = "SpecialityKindsName";
                        listSpeciality.ValueMember = "SpecialityKindsNO";
                        listSpeciality.DataSource = dt;
                    }
                }
            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                dt.Rows.Add(new object[] { "'1303010_A','1303010_B','1303010_C'", "表演(声台形" });
                dt.Rows.Add(new object[] { "'1303010_D'", "表演(表)" });
                //dt.Rows.Add(new object[] { "'1303090_A'", "播音_图片评述" });
                //dt.Rows.Add(new object[] { "'1303090_B'", "播音_文稿、才艺" });
                dt.Rows.Add(new object[] { "'1303090_A','1303090_B'", "播音主持" });
                listSpeciality.DataSource = dt;
                listSpeciality.DisplayMember = "name";
                listSpeciality.ValueMember = "id";
            }
        }

        private void BindExamPlace()
        {
            using (DataTable dt = service.GetExamPlaceTableALL_CS())
            {
                listExamPlace.DisplayMember = "ExamPlaceName";
                listExamPlace.ValueMember = "ExamPlaceNO";
                listExamPlace.DataSource = dt;
            }
        }




        private void BindExamLevel()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Rows.Add(new object[] { "1", "初试" });


            listExamLevel.DataSource = dt;
            listExamLevel.DisplayMember = "name";
            listExamLevel.ValueMember = "id";






            //if (listExamPlace.SelectedIndex < 0)
            //{
            //    return;
            //}
            //if (listSpeciality.SelectedIndex < 0)
            //{
            //    return;
            //}
            //string ExamPlaceID = string.Empty;
            //string ExamPlaceName = string.Empty;
            //if (listExamPlace.SelectedItem != null)
            //{
            //    DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
            //    ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
            //    ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            //}
            //string SpecialityKindsNO = string.Empty;
            //if (listSpeciality.SelectedItem != null)
            //{
            //    DataRowView drExamPlace = (DataRowView)listSpeciality.SelectedItem;
            //    SpecialityKindsNO = drExamPlace.Row.ItemArray[0].ToSafeString();
            //}
            //using (DataTable dt = service.GetExamLevelByPlaceID_Speciality_CS(ExamPlaceID, SpecialityKindsNO))
            //{
            //    //dt.Rows.Remove(dt.Select("DicNO='0'")[0]);
            //    listExamLevel.DisplayMember = "DicName";
            //    listExamLevel.ValueMember = "DicNO";
            //    listExamLevel.DataSource = dt;
            //}
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listExamPlace_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExamSpecNo();
        }

        private void listSpeciality_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExamLevel();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (listExamPlace.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考点!");
                return;
            }
            if (listSpeciality.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择专业!");
                return;
            }
            if (listExamLevel.SelectedIndex < 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择级别!");
                return;
            }
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            string SpecialityKindsNO = string.Empty;
            string SpecialityKindsName = string.Empty;
            string ExamLevel = string.Empty;
            string ExamLevelName = string.Empty;
            if (listExamPlace.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)listExamPlace.SelectedItem;
                ExamPlaceID = drExamPlace.Row.ItemArray[0].ToSafeString();
                ExamPlaceName = drExamPlace.Row.ItemArray[1].ToSafeString();
            }
            if (listSpeciality.SelectedItem != null)
            {

                if (strSelectType == "")
                {

                    if (listSpeciality.SelectedItems.Count > 1)
                    {
                        DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection selectItems = listSpeciality.SelectedItems;
                        for (int i = 0; i < selectItems.Count; i++)
                        {
                            DataRowView dr = (DataRowView)selectItems[i];
                            if (SpecialityKindsNO.Length == 0)
                            {
                                SpecialityKindsNO = dr[0].ToSafeString();
                                SpecialityKindsName = dr[1].ToSafeString();
                            }
                            else
                            {
                                SpecialityKindsNO = SpecialityKindsNO + "," + dr[0].ToSafeString();
                                SpecialityKindsName = SpecialityKindsName + "," + dr[1].ToSafeString();
                            }
                        }
                    }
                    else
                    {
                        DataRowView drExamLevel = (DataRowView)listSpeciality.SelectedItem;
                        SpecialityKindsNO = drExamLevel.Row.ItemArray[0].ToSafeString();
                        SpecialityKindsName = drExamLevel.Row.ItemArray[1].ToSafeString();
                    }
                }
                else
                {
                    DataRowView drExamLevel = (DataRowView)listSpeciality.SelectedItem;
                    SpecialityKindsNO = drExamLevel.Row.ItemArray[0].ToSafeString();
                    SpecialityKindsName = drExamLevel.Row.ItemArray[1].ToSafeString();
                }
            }
            if (listExamLevel.SelectedItem != null)
            {
                DataRowView drExamLevel = (DataRowView)listExamLevel.SelectedItem;
                ExamLevel = drExamLevel.Row.ItemArray[0].ToSafeString();
                ExamLevelName = drExamLevel.Row.ItemArray[1].ToSafeString();
            }
            SessionerHelper.ExaminationID = SessionerHelper.ExamLevel = SessionerHelper.ExamLevelName =
                SessionerHelper.ExamPlaceID = SessionerHelper.ExamPlaceName =
                SessionerHelper.ExamRoomID = SessionerHelper.ExamRoomName =
                SessionerHelper.SpecialityKinds = SessionerHelper.SpecialityKindsNO = string.Empty;





            SessionerHelper.SpecialityKinds = SpecialityKindsName;
            SessionerHelper.SpecialityKindsNO = SpecialityKindsNO;
            SessionerHelper.ExamLevel = ExamLevel;
            SessionerHelper.ExamLevelName = ExamLevelName;
            SessionerHelper.ExamPlaceID = ExamPlaceID;
            SessionerHelper.ExamPlaceName = ExamPlaceName;
            TB_Exam_Entretenimientoh model = service.GetEntretenimientohModel(ExamLevel, ExamPlaceID, SpecialityKindsNO);
            if (model != null)
            {
                SessionerHelper.ExaminationID = model.EntretenimientohID;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
